package game.tycoon.sosial;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import android.view.VelocityTracker;
import java.util.ArrayList;
import java.util.Random;

public class WorldView extends SurfaceView implements SurfaceHolder.Callback {
	Paint paint = new Paint();
	
	boolean onRestart = false;
	boolean showPlayerList = true;

	public WorldThread thread;
	public WorldActivity mWorldActivity;
	private static final String TAG = WorldView.class.getSimpleName();
	public PeopleController allPeople;

	private static final int fullTime = 86400; // batas waktu satu hari
	private int currentTime = 0; // waktu saat ini
	
	
	// jumlah player lokal
	private int numOfPlayer = WorldSetting.numPlayer;
	
	// player turn
	private int Turn;
	
	// matriks digunakan untuk geser dan zoom
	Matrix matrix = new Matrix();
	Matrix savedMatrix = new Matrix();
	Matrix Inv = new Matrix();
	Matrix Pow = new Matrix();
	
	// status touch
	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	static final int SHOOT = 3;
	static final int FLING = 4;	
	static final int FOCUS = 5;
	static final int WAIT_TO_FOCUS=6;
	
	// penanda untuk highlight garis
	static final String H_GARISBAWAH = "People yang paling jauh mulai paling duluan";
	static final String H_GARISAPOLLO = "Apollo dimulai!!!";
	static final String H_GARISBULETAN = "Buletan dimulai!!!";	

	// penanda kemungkinan double tap
	private boolean preDoubleTap = false;
	// counter double tap
	private int doubleTapCounter = 0;
	// penanda mulai double tap
	private boolean DoubleTaped = false;
	// maksimum counter tap
	private int tapMaxCounter = 100;
	
	//counter animasi marker(penanda gundu ini yang lagi jalan)
    int shouted = -1;
	static final int M_SHOUT = 0;
	static final int P_SHOUT = 1;
	static final int H_SHOUT = 2;    
    int cshouted = 0;//counter animasi marker gundu
    int ctimeshouted = 0;
    private Bitmap animShout;
    private int timeBetweenAnimShout = 3;//waktu untuk pindah ke frame berikutnya
	
    //animas marker
	public static Bitmap[][] animShouter;
	public boolean PeopleFocus = false;
	
	// kondisi menang
	private boolean Finish = false;
	
	// array kepemiikan bola yang berhasil diraih
	public int Operasi[][];
	public int Urutan[];
	
	// toast mulai permainan
	private boolean TMulai = false;
	// toast kondisi menang
	private boolean TMenang = false;
	// toast End
	private boolean TDuel = false;
	
	// penentu tutorial
	private int Dialog = DLEMPAR;
	static final int DLEMPAR = 1;
	static final int DMAIN = 2;
	static final int DDUEL = 3;
	private boolean RELEASE_SHOOT = false;
	private boolean THROWING = false;
	private boolean INITLEMPAR = false;
	int mode = NONE;

	// titik tengah jarak antar dua jari
	private PointF mid = new PointF(0, 0);
	// jarak antara dua jari
	private float oldDist;
	// titik mula2 touch pertama ditekan
	private PointF start = new PointF(0, 0);
	// titik akhir touch ditekan
	private PointF finish = new PointF(0, 0);
	// titik akhir touch ditekan
	private PointF release = new PointF(0, 0);
	// gambar latar belakang
	private Bitmap mBackground;
	// velocitytracker digunakan untuk menghitung kecepatan fling
	private VelocityTracker mVelocityTracker;
	// besar skalar kecepatan fling
	private int Vfling = 0;
	// maksimum kecepatan fling
	private static final int maxVfling = 40;
	// sudut fling
	private double fangle;
	// ukuran layar hape
	public int screenWidth;
	public int screenHeight;

	// player pemenang
	private int IDPemenang = -9999;

	// batas minimum scale layar
	public float minScale;
	// batas maksimum scale layar
	public float maxScale;
	// gambar power
//	private Bitmap power = BitmapFactory.decodeResource(getResources(),
//			R.drawable.power);
//	private Bitmap power_release = BitmapFactory.decodeResource(getResources(),
//			R.drawable.power_release);
	private float PowerLempar;
	private int maxPowerLempar = 25;
	// createa matrix for the power meter
	Matrix powerMtr = new Matrix();
	// createa matrix for hud player
	Matrix hudMtr = new Matrix();
	// simpenan rotasi
	private float Rotate = 0;
//	Bitmap powerTrans = BitmapFactory.decodeResource(getResources(), R.drawable.power);
	Bitmap PowerBar;
	// kesalahan pemain
	private int fault = 0;
	// array buat list bola yang kalah karena tertabrak
	private ArrayList<Integer> delete = new ArrayList<Integer>();
	/*
	 * state game() : RUNNING, PAUSE
	 */
	public int state = RUNNING;
	public static final int PAUSE = 0;
	public static final int RUNNING = 1;

	public static final int INIT = 0;
	public static final int PLAY = 1;
	public static final int ENDING = 2;
	private int GAME_STATE = INIT;
	
	/*
	 * konstanta untuk garis yang dihighlight saat tutorial in-game
	 */
	public final static int GARIS_BATAS_BAWAH = 0;
	public final static int GARIS_BULETAN = 1;
	public final static int GARIS_SEGITIGA_APOLLO = 2;
	public final static int GARIS_ROKET_APOLLO = 3;	
	
	//counter animasi highlight
	private int cHighlight = 0;
	//batas counter animasi highlight(highlight dimatikan begitu counter highlight mencapai angka ini)
	private final static int maxCounterHighlight = 150;//berarti sekitar 3 detik(jika memakai 60 fps)
	//penanda highlight garis batas bawah diaktifkan atau tidak
	private boolean statusHighlightGarisBatasBawah = false;
	//penanda highlight garis buletan diaktifkan atau tidak
	private boolean statusHighlightGarisBuletan = false;
	//penanda highlight garis segitiga apollo diaktifkan atau tidak
	private boolean statusHighlightGarisSegitigaApollo = false;
	//penanda highlight garis roket diaktifkan atau tidak
	private boolean statusHighlightGarisRoketApollo = false;
	//waktu untuk pindah ke frame gambar animasi highlight berikutnya
	private int timeBetweenAnimHighlight = 3;
	//counter waktu buat nentuin pindah ke frame gambar animasi highlight berikutnya
	private int cTimeAnimHighlight;
	//gambar garis highlight batas bawah
	private Bitmap[] animBatasBawah;
	private int cAnimBatasBawah;
	//gambar garis highlight buletan dan counter animasi
	private Bitmap[] animBuletan;
	private int cAnimBuletan;
	//gambar garis highlight segitiga apollo
	private Bitmap[] animSegitigaApollo;
	private int cAnimSegitigaApollo;
	//gambar garis highlight batas bawah
	private Bitmap[] animRoketApollo;
	private int cAnimRoketApollo;
	
	// gambar untuk power
//	private Bitmap power_hijau = BitmapFactory.decodeResource(getResources(),
//			R.drawable.garis_power_hijau);
//	private Bitmap power_kuning = BitmapFactory.decodeResource(getResources(),
//			R.drawable.garis_power_kuning);
//	private Bitmap power_merah = BitmapFactory.decodeResource(getResources(),
//			R.drawable.garis_power_merah);
	// penanda on click hud pemain
	private boolean hud_touch = false;
	private int hud_status = hud_normal;
	private int hudCounter = 0;
	private Bitmap [] hudImage;
	private Bitmap [] apatar_lib;
	static final int hud_normal =0;
	static final int hud_right = 1;
	static final int hud_left = 2;
	private int MaxPemainHud = 5;
	/*
	 * gambar2 untuk menu pause
	 */
//	private Bitmap dim = BitmapFactory.decodeResource(getResources(),
//			R.drawable.pausebg);
//	private Bitmap play_btn = BitmapFactory.decodeResource(getResources(),
//			R.drawable.play);
//	private Bitmap restart_btn = BitmapFactory.decodeResource(getResources(),
//			R.drawable.restart);
//	private Bitmap pause_btn = BitmapFactory.decodeResource(getResources(),
//			R.drawable.pause_kotak_edited);
//	private Bitmap backToMenu_btn = BitmapFactory.decodeResource(
//			getResources(), R.drawable.back_to_menu2);
//	private Bitmap sound_btn = BitmapFactory.decodeResource(getResources(),
//			R.drawable.sound_button2);
//	private Bitmap soundOff_btn = BitmapFactory.decodeResource(getResources(),
//			R.drawable.sound_off_button2);
	int offsetBetweenButtonAtPause = 20;
	int pauseSize = 30;
	/*
	 * gambar untuk hud player
	 */
//	private Bitmap hudPemain = BitmapFactory.decodeResource(getResources(),
//			R.drawable.panelgeser);
//	
//	// button lain2
//	private Bitmap lain2_btn = BitmapFactory.decodeResource(getResources(),R.drawable.lain2_edited);
	int lain2Size = 30;

	// beberapa atribut drag shoot
	int maxPower = 7000;
	int powerScaleFactor = 70;
	
	// atribut untuk animasi lemparan
	float csize = 0;//counter scale
	float vAwalBola = 0;//kecepatan awal bola saat dilempar
	boolean membesar;//menentukan apa saatnya bola membesar atau mengecil
	float percepatanScale=0.25f;//percepatan scale bola
	float kecepatanScale=0;//kecepatan scale
	
	// kecepatan slide focus
	int focusV = 20;
	// sudut untuk slide focus
	private double focusAngle;
	//lama waktu untuk mulai slide focus
	private static int waitFocusTime=50;
	//counter waktu untuk mulai slide focus
	private int cWaitFocus;
	
	public WorldView(Object context, int screenWidth, int screenHeight) {
		super((Context) context);
		mWorldActivity = (WorldActivity) context;
		getHolder().addCallback(this);
		setFocusable(true);
		// instansiasi bg sesuai dengan MAP_ID
		Options options = new BitmapFactory.Options();
		options.inScaled = false;

		if(WorldSetting.businessArea == WorldSetting.ROTI) {//jika mode-nya latihan otomatis map yang dipilih adalah nomor 2
			WorldSetting.playWorld = 1;
		}		
		mBackground = BitmapFactory.decodeResource(getResources(),WorldMap.getBackground(WorldSetting.playWorld), options);
		// ambil informasi lebar dan panjang layar

		this.screenWidth = screenWidth;
		this.screenHeight = screenHeight;
		// hitung maksimum scaling
		if (screenWidth / mBackground.getWidth() < screenHeight
				/ mBackground.getHeight()) {
			minScale = 1.0f * screenWidth / mBackground.getWidth();
		} else {
			minScale = 1.0f * screenHeight / mBackground.getHeight();
		}
		// set scale pertama kali ke paling jauh(scale paling minimum)
		matrix.setScale(minScale, minScale);
		// set scale maksimum
		maxScale = 1.5f;
		allPeople = new PeopleController((Context) context, getResources(),mBackground.getWidth(), mBackground.getHeight(),WorldSetting.businessArea, 1);
		allPeople.addPeople();
		
//		hudImage = new Bitmap[WorldSetting.numPlayer];
//		// instansiasi gunducontroller
//		allPeople = new PeopleController((Context) context, getResources(),mBackground.getWidth(), mBackground.getHeight(),WorldSetting.modegame,(WorldSetting.taruhan * WorldSetting.numPlayer));
//		Operasi = new int[WorldSetting.numPlayer][2];
//		Urutan = new int [WorldSetting.numPlayer];
//		for (int n = 0; n < WorldSetting.numPlayer; n++) {
//			Operasi[n][0] = 0;// banyaknya kelereng yang telah didapat
//		}
//		for (int n = 0; n < WorldSetting.numPlayer; n++) {
//			Operasi[n][1] = 1;// status 1 = masih hidup, status 0 sudah mati
//		}
//		Log.d(TAG, "kelereng jumlah : " + WorldSetting.taruhan);
//		//jika tutorial in-game di-enabled, inisialisasi gambar2 tutorial
//	   	//load gambar marker
//		initializeShoutBitmap();
//		initializeApatar();
//    	if(WorldSetting.statusTutorial) {
//			initializeHighlightBitmap();
//		}
//		if (WorldSetting.modegame==WorldSetting.LATIHAN){
//			GAME_STATE = PLAY;
//			Turn = 0 ;
//			allPeople.ChangePeople(Turn, 1);
//		}
//		else{
//			GAME_STATE = INIT;
//		}

	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// inisialisasi thread
		initThread();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// jika view di-destroy, thread harus di-stop
		release();
		Log.d(TAG, "surface destroyed");
	}

	// inisialisasi thread
	public void initThread() {
		if (thread == null || !thread.isAlive()) {// jika belom diinisialisasi
													// threadnya atau threadnya
													// sudah tidak hidup lagi
			// instansiasi thread utama
			thread = new WorldThread(getHolder(), this);
			thread.start();
		}
		thread.setRunning(true);
	}

	// giliran pemain
	public void PlayerTurn() {
		int temporary = Urutan[0];
		for (int n = 0; n < WorldSetting.numPlayer-1; n++) {
			Urutan[n] = Urutan[n+1];
		}
		Urutan[WorldSetting.numPlayer-1] = temporary;

		if (WorldSetting.modegame != WorldSetting.DUEL) {
			if ((Turn - 1) >= 0) {
				if (allPeople.getPeople(Turn - 1).isPlayer() == 0) {
					Turn = allPeople.listOfPeople.size() - 1;
				} else {
					Turn -= 1;
				}
			} else {
				Turn = allPeople.listOfPeople.size() - 1;
			}
		} else {
			if ((Turn + 1) == allPeople.listOfPeople.size()) {
				Turn = 0;
			} else {
				Turn += 1;
			}
		}		
		//slide focus ke gundu yang lagi giliran sekarang
		mode = WAIT_TO_FOCUS;
		//set sudut increment-nya
		float[] tesm = new float[9];
		matrix.getValues(tesm);
		//set sudut slide focus
		focusAngle = Math.atan2((-tesm[Matrix.MSCALE_Y]*allPeople.getPeople(Turn).y) - (tesm[Matrix.MTRANS_Y]-tesm[Matrix.MSCALE_Y]*screenHeight/2),(-tesm[Matrix.MSCALE_X]*allPeople.getPeople(Turn).x) - (tesm[Matrix.MTRANS_X]-tesm[Matrix.MSCALE_X]*screenWidth/2));
	}

	// fungsi dipanggil kalo view di-destroy
	private void release() {
		boolean retry = true;
		while (retry) {
			try {
				thread.join();
				retry = false;
				thread = null;
			} catch (InterruptedException e) {
			}
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		final int actioncode = event.getAction() & MotionEvent.ACTION_MASK;
		if (state != PAUSE) {// jika game sedang tidak pause
			int i;
			double ex, ye, D;
			if (mVelocityTracker == null) {
				mVelocityTracker = VelocityTracker.obtain();
			}
			mVelocityTracker.addMovement(event);
			switch (actioncode) {
			case MotionEvent.ACTION_POINTER_DOWN:
//				i = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
//				oldDist = calcDist(event);
//				if (oldDist > 10f) {
//					savedMatrix.set(matrix);
//					calcMid(mid, event);
//					mode = ZOOM;
//
//				}
//				Log.d(TAG,"pointer down at " + event.getX(i) + " "+ event.getY(i));				
				break;				
			case MotionEvent.ACTION_DOWN:
				Log.d(TAG, "down at " + event.getX() + " " + event.getY());
				Log.d(TAG, "people at " + allPeople.getPeople(0).y + " " + allPeople.getPeople(0).y);
				Matrix m = new Matrix();
				matrix.invert(m);
				float[] test = new float[2];
				test[0] = event.getX();
				test[1] = event.getY();
				m.mapPoints(test);
				//allPeople.moveTo((int)test[0],(int)test[1]);
				
				
//				Log.d(TAG,"kordinat mesin : " + test[0] + " " + test[1]);				
//				PeopleFocus = true;
//				if(event.getX() > 0 && event.getX() < pauseSize
//						&& event.getY() > screenHeight - pauseSize
//						&& event.getY() < screenHeight) {
//					// jika memencet tombol pause
//					state = PAUSE;					
//				}else if (WorldSetting.modegame!=WorldSetting.LATIHAN &&event.getX() > screenWidth - lain2Size
//						&& event.getX() < screenHeight
//						&& event.getY() > screenHeight - lain2Size
//						&& event.getY() < screenHeight) {
//					// jika memencet tombol titik tiga(tombol titik tiga tidak ada di mode latihan)
//					showPlayerList = !showPlayerList;					
//				}else if (showPlayerList && event.getX() > 0 
//						&& event.getX() < mBackground.getWidth()/9
//						&& event.getY() > mBackground.getHeight()/85
//						&& event.getY() < (mBackground.getHeight()/85)+90) {					
//					Log.d("CEK HUD", "HUD KEPENCET");
//					// jika memencet tombol HUD
//					start.set(event.getX(), event.getY());
//					finish.x = event.getX()+10;
//					hud_touch = true;
//				}else{
//					matrix.invert(Inv);
//					float[] kordinatmesin1 = { (float) event.getX(),
//							(float) event.getY() };
//					Inv.mapPoints(kordinatmesin1);
//					//PAKE KODE INI BUAT DEBUG SEGITIGA
//					//if(allPeople.PointinApollo((int)kordinatmesin1[0], (int)kordinatmesin1[1])){
//						//ShowToast("di apollo, kiri = "+allPeople.Kiri_Atas.x+"kanan="+allPeople.Kanan_Atas.x+"bawah="+allPeople.Kanan_Bawah.y+"atas="+allPeople.Kanan_Atas.y+". sedangkan x="+kordinatmesin1[0]+",y="+kordinatmesin1[1]);
//					//}
//					if ((GAME_STATE == INIT) && (WorldSetting.modegame!=WorldSetting.LATIHAN)){
//						if (allPeople.NoPeopleMoving()) {
//							// mode=InitBuletan;
//							INITLEMPAR = true;
//							PowerLempar = 0;
//						}
//					}
//					// siapin matrix buat drag
//					savedMatrix.set(matrix);
//					// titik awal drag
//					start.set(event.getX(), event.getY());
//					// ubah format kordinat si gundu di mesin menjadi kordinat
//					// di
//					// layar
//					if ((GAME_STATE == PLAY) || (GAME_STATE == ENDING)) {
//						//ShowToast("double tap masuk nih");
//						if (!preDoubleTap){
//							preDoubleTap = true;
//							//ShowToast("double tap mulai");
//						}
//						else{
//							if (doubleTapCounter<tapMaxCounter){
//								DoubleTaped = true;
//							}
//						}
//						if (allPeople.PeopleExist()) {
//							float[] transCoord = {
//									(float) allPeople.getPeople(Turn)
//											.getCenterX(),
//									(float) allPeople.getPeople(Turn)
//											.getCenterY() };
//							matrix.mapPoints(transCoord);
//
//							ex = event.getX() - transCoord[0];
//							ye = event.getY() - transCoord[1];
//							D = ex * ex + ye * ye;
//							D = Math.sqrt(D);
//							if ((D <= allPeople.getPeople(Turn).getBitmap()
//									.getWidth() / 2)
//									&& (allPeople.NoPeopleMoving())) {
//								mode = SHOOT;
//							} else {
//								mode = DRAG;
//							}
//						} else {
//							mode = DRAG;
//						}
//					} else {
//						mode = DRAG;
//					}
//				}
				break;
			case MotionEvent.ACTION_MOVE:
//				if (mode == DRAG) {
//					matrix.set(savedMatrix);
//					matrix.postTranslate(event.getX() - start.x, event.getY()
//							- start.y);
//					float[] tesm = new float[9];
//					matrix.getValues(tesm);
//					if (tesm[Matrix.MTRANS_X] > 0 && tesm[Matrix.MTRANS_Y] > 0) {// pergeseran
//																					// melebihi
//																					// batas
//																					// atas
//																					// dan
//																					// batas
//																					// kiri
//						matrix.postTranslate(0, -tesm[Matrix.MTRANS_Y]);
//						matrix.postTranslate(-tesm[Matrix.MTRANS_X], 0);
//					} else if (tesm[Matrix.MTRANS_X] < -mBackground.getWidth()
//							* tesm[Matrix.MSCALE_X] + screenWidth
//							&& tesm[Matrix.MTRANS_Y] < -mBackground.getHeight()
//									* tesm[Matrix.MSCALE_Y] + screenHeight) {// pergeseran
//																				// melebihi
//																				// batas
//																				// bawah
//																				// dan
//																				// batas
//																				// kanan
//						matrix.postTranslate(0, -mBackground.getHeight()
//								* tesm[Matrix.MSCALE_Y] + screenHeight
//								- tesm[Matrix.MTRANS_Y]);
//						matrix.postTranslate((-mBackground.getWidth()
//								* tesm[Matrix.MSCALE_X] + screenWidth)
//								- tesm[Matrix.MTRANS_X], 0);
//					} else if (tesm[Matrix.MTRANS_Y] < -mBackground.getHeight()
//							* tesm[Matrix.MSCALE_Y] + screenHeight
//							&& tesm[Matrix.MTRANS_X] > 0) {// pergeseran
//															// melebihi batas
//															// bawah dan batas
//															// kiri
//						matrix.postTranslate(0, -mBackground.getHeight()
//								* tesm[Matrix.MSCALE_Y] + screenHeight
//								- tesm[Matrix.MTRANS_Y]);
//						matrix.postTranslate(-tesm[Matrix.MTRANS_X], 0);
//					} else if (tesm[Matrix.MTRANS_X] < -mBackground.getWidth()
//							* tesm[Matrix.MSCALE_X] + screenWidth
//							&& tesm[Matrix.MTRANS_Y] > 0) {// pergeseran
//															// melebihi batas
//															// kanan dan batas
//															// atas
//						matrix.postTranslate((-mBackground.getWidth()
//								* tesm[Matrix.MSCALE_X] + screenWidth)
//								- tesm[Matrix.MTRANS_X], 0);
//						matrix.postTranslate(0, -tesm[Matrix.MTRANS_Y]);
//					} else if (tesm[Matrix.MTRANS_X] > 0) {// pergeseran
//															// melebihi batas
//															// kiri map
//						matrix.postTranslate(-tesm[Matrix.MTRANS_X], 0);
//					} else if (tesm[Matrix.MTRANS_Y] > 0) {// pergeseran
//															// melebihi batas
//															// atas map
//						matrix.postTranslate(0, -tesm[Matrix.MTRANS_Y]);
//					} else if (tesm[Matrix.MTRANS_X] < -mBackground.getWidth()
//							* tesm[Matrix.MSCALE_X] + screenWidth) {// pergeseran
//																	// melebihi
//																	// batas
//																	// kanan map
//						matrix.postTranslate((-mBackground.getWidth()
//								* tesm[Matrix.MSCALE_X] + screenWidth)
//								- tesm[Matrix.MTRANS_X], 0);
//					} else if (tesm[Matrix.MTRANS_Y] < -mBackground.getHeight()
//							* tesm[Matrix.MSCALE_Y] + screenHeight) {// pergeseran
//																		// melebihi
//																		// batas
//																		// bawah
//																		// map
//						matrix.postTranslate(0, -mBackground.getHeight()
//								* tesm[Matrix.MSCALE_Y] + screenHeight
//								- tesm[Matrix.MTRANS_Y]);
//					}
//				} else if (mode == ZOOM) {
//					float newDist = calcDist(event);
//					if (newDist > 10f) {
//						matrix.set(savedMatrix);
//						float scale = newDist / oldDist;
//						float[] tesm = new float[9];
//						matrix.getValues(tesm);
//						if (tesm[Matrix.MSCALE_X] * scale > minScale
//								&& tesm[Matrix.MSCALE_X] * scale < maxScale) {// jika
//																				// scaling
//																				// lebih
//																				// dari
//																				// dari
//																				// minimum
//																				// scaling
//																				// dan
//																				// kurang
//																				// dari
//																				// maksimum
//																				// scaling
//							matrix.postScale(scale, scale, mid.x, mid.y);
//						} else if (tesm[Matrix.MSCALE_X] * scale > maxScale) {// jika
//																				// melebihi
//																				// maximum
//																				// scale
//							matrix.postScale(maxScale / tesm[Matrix.MSCALE_X],
//									maxScale / tesm[Matrix.MSCALE_Y], mid.x,
//									mid.y);
//						} else if (tesm[Matrix.MSCALE_X] * scale < minScale) {// jika
//																				// kurang
//																				// dari
//																				// minimum
//																				// scale
//							matrix.setTranslate(0, 0);
//							matrix.postScale(minScale, minScale);
//						}
//					}
//				} else if ((mode == SHOOT)||(hud_touch)) {
//					finish.x = event.getX();
//					finish.y = event.getY();
//				}
				break;
			case MotionEvent.ACTION_UP:
//				double shoot_x;
//				double shoot_y;
//				if (hud_touch){
//					if (event.getX()>mBackground.getWidth()/2){
//						hud_status = hud_right;
//					}
//					else{
//						hud_status = hud_left;
//					}
//					start.x = 0;
//					finish.x = 0;
//					hud_touch = false;
//					hudCounter = 0;
//				}
//				if (INITLEMPAR) {
//					if (WorldSetting.modegame != WorldSetting.DUEL) {						
//						if (allPeople.NoPeopleMoving()) {
//							matrix.invert(Inv);
//							float[] kordinatmesin = { (float) event.getX(),
//									(float) event.getY() };
//							Inv.mapPoints(kordinatmesin);
//							allPeople.InsertPlayer(
//									(WorldSetting.numPlayer - numOfPlayer) + 1,
//									0, 0, (int) kordinatmesin[0], 0);							
//							// sesuaiin dengan maksimum lemparan
//							if (PowerLempar > maxPowerLempar) {
//								PowerLempar = maxPowerLempar;
//							}
//							allPeople.getPeople(allPeople.listOfPeople.size() - 1).yinc = PowerLempar;
//							//untuk animasi lemparan
//							this.vAwalBola = PowerLempar;
//							membesar = true; // besarin bola sampai ke tengah perjalanan
//							csize = 0;//set counter skala bola ke 0
//							kecepatanScale = 0;//reset kecepatan
//							Log.d(TAG, "kekuatan lemparan=" + PowerLempar);							
//							THROWING = true;
//							INITLEMPAR = false;
//						}
//					} else {
//						// Toast.makeText(mWorldActivity,
//						// "Letakkan Kelereng di posisi yang sesuai",
//						// Toast.LENGTH_SHORT).show();
//						matrix.invert(Inv);
//						float[] kordinatmesin = { (float) event.getX(),
//								(float) event.getY() };
//						Inv.mapPoints(kordinatmesin);
//						allPeople.InsertPlayer(
//								(WorldSetting.numPlayer - numOfPlayer) + 1,
//								0, 0, (int) kordinatmesin[0],
//								(int) kordinatmesin[1]);
//						numOfPlayer -= 1;
//						THROWING = true;
//						INITLEMPAR = false;
//					}
//				}
//				// else{
//				if (mode == SHOOT) {
//					shoot_x = (start.x - event.getX());
//					shoot_y = (start.y - event.getY());
//					Log.d(TAG, "up at " + event.getX() + " " + event.getY()
//							+ ". soot= " + shoot_x + "," + shoot_y);
//					// hitung sudut shoot
//					double angleshoot = Math.atan2(shoot_y, shoot_x);
//					double shoot = Math.sqrt(shoot_x * shoot_x + shoot_y
//							* shoot_y)
//							* powerScaleFactor;// cari besar shoot
//					// sesuain dengan maxpower yang diperbolehkan
//					if (shoot > maxPower) {
//						shoot = maxPower;
//					}
//					// pecahin lagi ke komponennya masing2 dengan sudut shoot
//					// sebelumnya
//					shoot_x = shoot * Math.cos(angleshoot);
//					shoot_y = shoot * Math.sin(angleshoot);
//					allPeople.getPeople(Turn).shoot(shoot_x, shoot_y, 5);
//					allPeople.ChangePeople(Turn, 0);
//					Log.d(TAG, "Turn 478 = " + Turn);
////					release.x = (float) (allPeople.listOfPeople.get(Turn).getCenterX() - powerTrans
////							.getWidth() / 2);
////					release.y = (float) (allPeople.listOfPeople.get(Turn).getCenterY() - powerTrans
////							.getHeight() / 2);
//					// mode = RELEASE_SHOOT;
//					RELEASE_SHOOT = true;
//					mode = NONE;
//				} else if (mode == DRAG) {
//					// hitung dan dapatkan kecepatan fling
//					mVelocityTracker.computeCurrentVelocity(1000);
//					Vfling = (int) (Math.sqrt(mVelocityTracker.getXVelocity()
//							* mVelocityTracker.getXVelocity()
//							+ mVelocityTracker.getYVelocity()
//							* mVelocityTracker.getYVelocity()));
//					fangle = Math.atan2(mVelocityTracker.getYVelocity(),
//							mVelocityTracker.getXVelocity());
//					// perkecil kecepatan yang didapat mVelocityTracker
//					Log.d(TAG, "kecepatan : " + Vfling * 0.08);
//					Vfling = (int) (0.08 * Vfling);
//					Log.d(TAG, "sudut : " + fangle);
//					// Log.d(TAG,"kecepatan : " + Vfling);
//					if (Vfling > maxVfling) {
//						Vfling = maxVfling;// mentokin ke kecepatan maksimum
//					}
//					mode = FLING;
//				}
//				fixScreen();// perbaikan layar(dikarenakan abis zoom, ada bagian
//							// luar map yang keliatan)
//							// }
				break;
			case MotionEvent.ACTION_POINTER_UP:
//				i = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
//				Log.d(TAG,
//						"pointer up at " + event.getX(i) + " " + event.getY(i));
//				mode = NONE;
				break;
			}
			try {
				Thread.sleep(32);// pause thread event sebanyak 50 ms (biar gak ngelag)
			} catch (InterruptedException e) {
			}
		} else {// jika game sedang pause
			switch (actioncode) {
			case MotionEvent.ACTION_DOWN:
//				if ((event.getX() > screenWidth / 2 - play_btn.getWidth() / 2)
//						&& event.getX() < screenWidth / 2 + play_btn.getWidth()
//								/ 2) {
//					if (event.getY() > screenHeight / 2
//							- offsetBetweenButtonAtPause / 2
//							- restart_btn.getHeight()
//							- offsetBetweenButtonAtPause - play_btn.getHeight()
//							&& event.getY() < screenHeight / 2
//									- offsetBetweenButtonAtPause / 2
//									- restart_btn.getHeight()
//									- offsetBetweenButtonAtPause) {
//						// button play ditekan
//						state = RUNNING;
//					} else if (event.getY() > screenHeight / 2
//							- offsetBetweenButtonAtPause / 2
//							- restart_btn.getHeight()
//							&& event.getY() < screenHeight / 2
//									- offsetBetweenButtonAtPause / 2) {
//						// button restart ditekan
//						onRestart = true;
//					} else if (event.getY() > screenHeight / 2
//							+ offsetBetweenButtonAtPause / 2
//							&& event.getY() < screenHeight / 2
//									+ offsetBetweenButtonAtPause / 2
//									+ backToMenu_btn.getHeight()) {
//						// button back to menu ditekan, balik ke pilih game mode
//						mWorldActivity.exitToGameMode();
//					} else if (event.getY() > screenHeight / 2
//							+ offsetBetweenButtonAtPause / 2
//							+ backToMenu_btn.getHeight()
//							+ offsetBetweenButtonAtPause
//							&& event.getY() < screenHeight / 2
//									+ offsetBetweenButtonAtPause / 2
//									+ backToMenu_btn.getHeight()
//									+ offsetBetweenButtonAtPause
//									+ sound_btn.getHeight()) {
//						// toggle sound
//						WorldSetting.soundenabled = !WorldSetting.soundenabled;
//						if (WorldSetting.soundenabled) {
//							GameSound.startGameMusic(mWorldActivity);
//						} else {
//							GameSound.stopGameMusic();
//						}
//					}
//				}
				break;
			}
		}
		return true;
	}

	private void fixScreen() {
		float[] tesm = new float[9];
		matrix.getValues(tesm);
		matrix.setScale(tesm[Matrix.MSCALE_X], tesm[Matrix.MSCALE_Y]);
		if (tesm[Matrix.MTRANS_X] > 0) {
			matrix.postTranslate(0, 0);
		} else if (tesm[Matrix.MTRANS_X] < -mBackground.getWidth()
				* tesm[Matrix.MSCALE_X] + screenWidth) {
			matrix.postTranslate(-mBackground.getWidth()
					* tesm[Matrix.MSCALE_X] + screenWidth, 0);
		} else {
			matrix.postTranslate(tesm[Matrix.MTRANS_X], 0);
		}
		if (tesm[Matrix.MTRANS_Y] > 0) {
			matrix.postTranslate(0, 0);
		} else if (tesm[Matrix.MTRANS_Y] < -mBackground.getHeight()
				* tesm[Matrix.MSCALE_Y] + screenHeight) {
			matrix.postTranslate(0, -mBackground.getHeight()
					* tesm[Matrix.MSCALE_Y] + screenHeight);
		} else {
			matrix.postTranslate(0, tesm[Matrix.MTRANS_Y]);
		}
	}

	private void calcMid(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

	private float calcDist(MotionEvent event) {
		float deltx = Math.abs(event.getX(0) - event.getX(1));
		float delty = Math.abs(event.getY(0) - event.getY(1));
		return FloatMath.sqrt(deltx * deltx + delty * delty);
	}

	protected void render(Canvas canvas) {
//		if (mode == FLING) {// jika sedang fling, aplikasikan transisi secara perlahan							
//			slideMap();
//		}		
//		
//		if(mode==FOCUS || mode==WAIT_TO_FOCUS) {//pergi menuju ke gundu yang lagi giliran
//			focusSlideToPeople();
//		}
//		if (RELEASE_SHOOT){
//			moveCamera((int)allPeople.getPeople(Turn).getCenterX() - screenWidth/2, (int)allPeople.getPeople(Turn).getCenterY()-screenHeight/2);
//		}
		canvas.setMatrix(matrix);
		drawBackGround(canvas);
		drawallPeople(canvas);
//		drawHand(canvas);
//		drawPower(canvas);
//		drawHighlight(canvas);
		drawHUD(canvas);
		drawPause(canvas);
//		drawShout(canvas);
//		if (THROWING) {
//			drawThrowing(canvas);
//		}
		//gambar power untuk lempar
		//untuk mode duel tidak butuh lemparan
//		if (INITLEMPAR && (WorldSetting.modegame==WorldSetting.BULETAN || WorldSetting.modegame == WorldSetting.APOLLO)) {			
//			canvas.setMatrix(null);
//			if (PowerLempar > maxPowerLempar) {
//				PowerLempar = maxPowerLempar;
//			}
////			if (PowerLempar < maxPowerLempar * 1.0 / 3) {
////				canvas.drawBitmap(
////						power_hijau,
////						null,
////						new Rect(
////								pauseSize + 10,
////								screenHeight - 10 - power_hijau.getHeight(),
////								(int) (PowerLempar
////										* ((screenWidth - lain2Size) - 10 - (pauseSize + 10)) / maxPowerLempar),
////								screenHeight - 10), null);
////			} else if (PowerLempar < maxPowerLempar * 2.0 / 3) {
////				canvas.drawBitmap(
////						power_kuning,
////						null,
////						new Rect(
////								pauseSize + 10,
////								screenHeight - 10 - power_hijau.getHeight(),
////								(int) (PowerLempar
////										* ((screenWidth - lain2Size) - 10 - (pauseSize + 10)) / maxPowerLempar),
////								screenHeight - 10), null);
////			} else if (PowerLempar >= maxPowerLempar * 2.0 / 3) {
////				canvas.drawBitmap(
////						power_merah,
////						null,
////						new Rect(
////								pauseSize + 10,
////								screenHeight - 10 - power_hijau.getHeight(),
////								(int) (PowerLempar
////										* ((screenWidth - lain2Size) - 10 - (pauseSize + 10)) / maxPowerLempar),
////								screenHeight - 10), null);
////			}
//			canvas.setMatrix(matrix);
//		}
	}

	private void animateLempar(Canvas canvas,People g) {
		float maxScale = 130f;//utak atik ini buat ngatur perbesaran maksimum kelereng
		Log.d(TAG,"maxScale : " + maxScale);
		if(g.yinc <= vAwalBola/2 ) {
			membesar = false;
		}
		if(membesar) {//besarin size gundu yang dilempar
			//csize+=2;
			csize+=kecepatanScale;
		}else{
			//csize-=2;
			csize-=kecepatanScale;
		}
		kecepatanScale += percepatanScale;
		if(csize<0) {//jika menjadi lebih kecil daripada biasanya set lagi jadi 0
			csize = 0;
		}
		if(csize>maxScale) {//jika csize kurang lebih sama dengan maxScale, jangan perbesar
			csize = maxScale;
			membesar = false;
		}
		Log.d(TAG,"scale : " + csize);
		g.drawScaledPeople(canvas, csize);//gambar gundu versi scale
	}
    private void drawShout(Canvas canvas) {
    	if (allPeople.NoPeopleMoving() && (shouted != (-1))){
    		canvas.setMatrix(null);
	    	animShout = animShouter[shouted][cshouted];
	    	canvas.drawBitmap(animShout,0,0, null);
	    	ctimeshouted++;//waktu berikutnya
	    	if (cshouted==2){
	    		if(ctimeshouted==(4*timeBetweenAnimShout)) {
		    		ctimeshouted = 0;
		    		cshouted += 1;
		    		if (cshouted==6){
		    			cshouted = 0;
		    			shouted = -1;
		    		}
		    	}
	    	}
	    	else{
	    		if(ctimeshouted==timeBetweenAnimShout) {
		    		ctimeshouted = 0;
		    		cshouted += 1;
		    		if (cshouted==6){
		    			cshouted = 0;
		    			shouted = -1;
		    		}
		    	}
	    	}
	    	canvas.setMatrix(matrix);
    	}
    }
	private void drawThrowing(Canvas canvas) {
		allPeople.getPeople(allPeople.listOfPeople.size() - 1).move(50);
		animateLempar(canvas,allPeople.getPeople(allPeople.listOfPeople.size()-1));
		if (allPeople.NoPeopleMoving()) {
			if (WorldSetting.modegame != WorldSetting.DUEL){
				if ((allPeople.getPeople(allPeople.listOfPeople.size() - 1).y < (mBackground
						.getHeight() / 3) + 650)
						) {
	
					allPeople.listOfPeople.remove(allPeople.listOfPeople.size() - 1);					
					if (fault == 2) {
						Random randomGenerator = new Random();
						int ran = randomGenerator.nextInt(mBackground.getWidth());
	
						allPeople.InsertPlayer(
								(WorldSetting.numPlayer - numOfPlayer) + 1, 0,
								0, ran, (mBackground.getHeight() / 3) + 650);
						numOfPlayer -= 1;
						fault = 0;
					} else {
						fault += 1;
					}
				}
				else{
					numOfPlayer -= 1;
					fault = 0;				
				}
			}
			mode = NONE;
			if (numOfPlayer == 0) {
				GAME_STATE = PLAY;
				if (WorldSetting.modegame!=WorldSetting.DUEL){
					for (int n = 0; n < WorldSetting.numPlayer; n++) {
						Urutan[n] = allPeople.getPeople((allPeople.listOfPeople.size()-n-1)).isPlayer()-1;// banyaknya kelereng yang telah didapat
						Log.d("Urutan1", "ini urutan ke-"+n+"="+(allPeople.listOfPeople.size()-n-1));
						Log.d("Urutan2", "urutan = "+ (allPeople.getPeople((allPeople.listOfPeople.size()-n-1)).isPlayer()-1));
					}
				}else{
					for (int n = 0; n < WorldSetting.numPlayer; n++) {
						Urutan[n] = allPeople.getPeople(n).isPlayer()-1;// banyaknya kelereng yang telah didapat
						Log.d("Urutan1", "ini urutan ke-"+n+"="+(allPeople.getPeople(n).isPlayer()-1));
						Log.d("Urutan2", "urutan = "+ (allPeople.getPeople(allPeople.getPeople(n).isPlayer()-1)));
					}					
				}
//				if (!TMulai) {
//					if (WorldSetting.modegame == 1) {
//						ShowToast("BULETAN DIMULAI");
//						mWorldActivity.showTutorial("DUEL DIMULAI KAWAN");
//					} else if (WorldSetting.modegame == 2) {
//						ShowToast("APOLLO DIMULAI");
//						mWorldActivity.showTutorial("DUEL DIMULAI KAWAN");
//					} else if (WorldSetting.modegame == 3) {
//						ShowToast("DUEL DIMULAI");						
//						mWorldActivity.showTutorial("DUEL DIMULAI KAWAN");
//					}
//					TMulai = true;
//				}
				if ((WorldSetting.modegame == WorldSetting.BULETAN)||(WorldSetting.modegame == WorldSetting.APOLLO)) {
					allPeople.PositioningPlayer();
					Turn = allPeople.listOfPeople.size() - 1;
				} else if (WorldSetting.modegame == WorldSetting.DUEL){
					Turn = 0;
				}
				Log.d(TAG,
						"Turn=" + Turn + ", Player="
								+ allPeople.getPeople(Turn).isPlayer());
				allPeople.ChangePeople(Turn, 1);
			}
			THROWING = false;
		}
	}

	private void ShowToast(final String text) {
		this.post(new Runnable() {
			public void run() {
				Toast.makeText(mWorldActivity, text, Toast.LENGTH_SHORT).show();
			}
		});

	}

	private void drawHUD(Canvas canvas) {
		if (state != PAUSE) {// jika sedang tidak pause, tampilkan button pause
								// di pojok kiri layar
			canvas.setMatrix(null);
//			if (WorldSetting.modegame != WorldSetting.LATIHAN){
				int spaceRemaining=0;
				if (showPlayerList) {
					Paint p = new Paint();
					
					p.setTextSize(10);
					p.setAntiAlias(true);
					p.setColor(Color.RED);

//					if (GAME_STATE == INIT){
//						spaceRemaining=0;
//						if (numOfPlayer!=0){
//							canvas.drawBitmap(hudImage[WorldSetting.numPlayer-numOfPlayer], hudCounter, mBackground.getHeight()/80, null);
//							for (int i = 0; i < WorldSetting.numPlayer;i++){//(allPeople.listOfPeople.size() - allPeople.CountPeopleNP()); i++) {
//								if((Operasi[i][1]==1)&&(i!=(WorldSetting.numPlayer-numOfPlayer))) {//jika player tersebut masih hidup baru tulis
//									canvas.drawBitmap(hudImage[i], hudCounter - ((spaceRemaining+1)*50), mBackground.getHeight()/80, null);
//									spaceRemaining+=1;
//								}
//							}
//						}
//					}
//					else if ((GAME_STATE == PLAY)||(GAME_STATE == ENDING)){
//						spaceRemaining=0;
//						canvas.drawBitmap(hudImage[allPeople.getPeople(Turn).isPlayer()-1], hudCounter, mBackground.getHeight()/80, null);
//						canvas.drawText(""+Operasi[allPeople.getPeople(Turn).isPlayer()-1][0], hudCounter+10,mBackground.getHeight()/80+75, p);
						canvas.drawText("Waktu yang tersisa = "+(fullTime-currentTime), hudCounter+10,mBackground.getHeight()/80+75, p);
						if (allPeople.listOfPeople.size()>0){
							canvas.drawText("X = "+(allPeople.getPeople(0).x)+", Y = "+(allPeople.getPeople(0).y), hudCounter+10,mBackground.getHeight()/80+175, p);
						}
//						for (int i = 0; i < WorldSetting.numPlayer;i++){//(allPeople.listOfPeople.size() - allPeople.CountPeopleNP()); i++) {
//							if((Operasi[i][1]==1)&&(i!=allPeople.getPeople(Turn).isPlayer()-1)) {//jika player tersebut masih hidup baru tulis
//								canvas.drawBitmap(hudImage[i], hudCounter- ((spaceRemaining+1)*50), mBackground.getHeight()/80, null);
//								canvas.drawText(""+Operasi[i][0], hudCounter - ((spaceRemaining+1)*50)+10, mBackground.getHeight()/80+75, p);
//								spaceRemaining+=1;
//							}
//						}
//					}										
//					if (!hud_touch){
//						if (hud_status==hud_right){
//							if (hudCounter<200){
//								hudCounter+=5;
//							}				
//							else{
//								hudCounter=200;
//								hud_status = hud_normal;
//							}
//						}
//						else if (hud_status==hud_left){
//							if (hudCounter>0){
//								hudCounter-=5;
//							}				
//							else{
//								hudCounter = 0;
//								hud_status = hud_normal;
//							}
//						}
//						else if (hud_status == hud_normal){
//							hud_status = hud_left;
//						}
//					}
//					else{
//						hudCounter = (int) (finish.x-start.x);
//					}
				}
//			}
			canvas.setMatrix(matrix);
		}
	}
	
	public String PlayerWinner() {
		String pemenang = "Tidak ada Pemenang";
		if (allPeople.FirstPlayer() != -9999) {
//			if (IDPemenang!=-9999)
//				pemenang = WorldSetting.PlayingEmployee.get(IDPemenang).name;
		}
		return pemenang;
	}

	public int PeoplePemenang() {
		int pemenang = 0;
		if (allPeople.FirstPlayer() != -9999) {
			pemenang = Operasi[IDPemenang][0];
		}
		return pemenang;
	}

	public String[] LoserPlayer() {
		String[] Looser = new String[WorldSetting.numPlayer-1]; // dikurangi 1 karena jumlah yang kalah adalah jumlah player keseluruhan diukrangi 1
		int k = 0;
		Log.d(TAG, "Player yang kalah dari indeks 0 = "
				+ (IDPemenang));
		for (int z = 0; z < WorldSetting.numPlayer; z++) {
			if (IDPemenang != z) {
				Log.d(TAG, "Player yang kalah dari indeks 0 = "
						+ (IDPemenang));
//				Looser[k] = WorldSetting.PlayingEmployee.get(z).name;
				k += 1;
			}
		}
		return Looser;
	}

	public int[] LoserPeople() {
		int[] Looser = new int[WorldSetting.numPlayer-1];
		int k = 0;
		for (int z = 0; z < WorldSetting.numPlayer; z++) {
			if (IDPemenang!= z) {
				Log.d(TAG, "Player yang kalah jumlah gundunya = "
						+ Operasi[z][0]);
				Looser[k] = Operasi[z][0];
				k += 1;
			}
		}
		return Looser;
	}

	private void drawHand(Canvas canvas) {
		float ChangeRotate = 0;
		if (mode == SHOOT) {
			float delta_x = finish.x - start.x;
			float delta_y = start.y - finish.y;
			if ((delta_x == 0) && (delta_y < 0)) {
				ChangeRotate = 270;
			} else if ((delta_x == 0) && (delta_y > 0)) {
				ChangeRotate = 90;
			} else if ((delta_x > 0) && (delta_y == 0)) {
				ChangeRotate = 180;
			} else if ((delta_x < 0) && (delta_y == 0)) {
				ChangeRotate = 0;
			} else if ((delta_x != 0) && (delta_y != 0)) {
				if ((delta_x < 0) && (delta_y > 0)) {
					ChangeRotate = (float) (Math.toDegrees(Math.atan2(delta_y,
							delta_x * -1)));
				}
				if ((delta_x > 0) && (delta_y > 0)) {
					ChangeRotate = 90 - (float) (Math.toDegrees(Math.atan2(
							delta_y, delta_x)));
					ChangeRotate += 90;
				}
				if ((delta_x > 0) && (delta_y < 0)) {
					ChangeRotate = (float) (Math.toDegrees(Math.atan2(delta_y
							* -1, delta_x)));
					ChangeRotate += 180;
				}
				if ((delta_x < 0) && (delta_y < 0)) {
					ChangeRotate = 90 - (float) (Math.toDegrees(Math.atan2(
							delta_y * -1, delta_x * -1)));
					ChangeRotate += 270;
				}
			}
			if ((ChangeRotate - Rotate) != 0) {
				powerMtr.postRotate(ChangeRotate - Rotate,
						(float) allPeople.listOfPeople.get(Turn).getCenterX(),
						(float) allPeople.listOfPeople.get(Turn).getCenterX());
				Rotate = ChangeRotate;
			}
//			powerTrans = Bitmap.createBitmap(power, 0, 0, power.getWidth(),
//					power.getHeight(), powerMtr, true);
//			canvas.drawBitmap(powerTrans, (float) (allPeople.listOfPeople.get(Turn)
//					.getCenterX() - powerTrans.getWidth() / 2),
//					(float) (allPeople.listOfPeople.get(Turn).getCenterY() - powerTrans
//							.getHeight() / 2), null);
		}
		if (RELEASE_SHOOT) {
//			powerTrans = Bitmap.createBitmap(power_release, 0, 0,
//					power_release.getWidth(), power_release.getHeight(),
//					powerMtr, true);
//			canvas.drawBitmap(powerTrans, release.x, release.y, null);
		}
	}

	private void drawPower(Canvas canvas) {
		canvas.setMatrix(null);		
		if (mode == SHOOT) {
			double wx;
			double wy;
			double distance;
			wx = (start.x - finish.x);
			wy = (start.y - finish.y);
			distance = Math.sqrt(wx * wx + wy * wy) * powerScaleFactor;
			if (distance > maxPower) {
				distance = maxPower;
			}
//			if (distance < maxPower * 1.0 / 3) {
//				canvas.drawBitmap(
//						power_hijau,
//						null,
//						new Rect(
//								pauseSize + 10,
//								screenHeight - 10 - power_hijau.getHeight(),
//								(int) (distance
//										* ((screenWidth - lain2Size) - 10 - (pauseSize + 10)) / maxPower),
//								screenHeight - 10), null);
//			} else if (distance < maxPower * 2.0 / 3) {
//				canvas.drawBitmap(
//						power_kuning,
//						null,
//						new Rect(
//								pauseSize + 10,
//								screenHeight - 10 - power_kuning.getHeight(),
//								(int) (distance
//										* ((screenWidth - lain2Size) - 10 - (pauseSize + 10)) / maxPower),
//								screenHeight - 10), null);
//			} else if (distance >= maxPower * 2.0 / 3) {
//				canvas.drawBitmap(
//						power_merah,
//						null,
//						new Rect(
//								pauseSize + 10,
//								screenHeight - 10 - power_merah.getHeight(),
//								(int) (distance
//										* ((screenWidth - lain2Size) - 10 - (pauseSize + 10)) / maxPower),
//								screenHeight - 10), null);
//			}
		}
		canvas.setMatrix(matrix);
	}
	
	private void slideMap() {
		Vfling--;
		// pecah jadi kecepatan komponen x dan y
		float Vx = (float) (Vfling * Math.cos(fangle));
		float Vy = (float) (Vfling * Math.sin(fangle));
		float tesm[] = new float[9];
		matrix.getValues(tesm);
		if (Vx + tesm[Matrix.MTRANS_X] > 0) {// pergeseran melebihi batas kiri
												// map
			matrix.setScale(tesm[Matrix.MSCALE_X], tesm[Matrix.MSCALE_Y]);
			matrix.postTranslate(0, tesm[Matrix.MTRANS_Y]);
		} else if (Vx + tesm[Matrix.MTRANS_X] < -mBackground.getWidth()
				* tesm[Matrix.MSCALE_X] + screenWidth) {// pergeseran melebihi
														// batas kanan map
			matrix.setScale(tesm[Matrix.MSCALE_X], tesm[Matrix.MSCALE_Y]);
			matrix.postTranslate(-mBackground.getWidth()
					* tesm[Matrix.MSCALE_X] + screenWidth,
					tesm[Matrix.MTRANS_Y]);
		} else {
			matrix.postTranslate(Vx, 0);
		}
		if (Vy + tesm[Matrix.MTRANS_Y] > 0) {// pergeseran melebihi batas atas
												// map
			matrix.setScale(tesm[Matrix.MSCALE_X], tesm[Matrix.MSCALE_Y]);
			matrix.postTranslate(tesm[Matrix.MTRANS_X], 0);
		} else if (Vy + tesm[Matrix.MTRANS_Y] < -mBackground.getHeight()
				* tesm[Matrix.MSCALE_Y] + screenHeight) {// pergeseran melebihi
															// batas bawah map
			matrix.setScale(tesm[Matrix.MSCALE_X], tesm[Matrix.MSCALE_Y]);
			matrix.postTranslate(tesm[Matrix.MTRANS_X],
					-mBackground.getHeight() * tesm[Matrix.MSCALE_Y]
							+ screenHeight);
		} else {
			matrix.postTranslate(0, Vy);
		}
		if (Vfling <= 0) {
			mode = NONE;
		}
	}

	private void drawPause(Canvas canvas) {
		if (state == PAUSE) {// jika sedang pause
			canvas.setMatrix(null);

			// redupin layar
//			canvas.drawBitmap(dim, 0, 0, null);
			// gambar tombol play
//			canvas.drawBitmap(play_btn, screenWidth / 2
//					- (play_btn.getWidth() / 2), screenHeight / 2
//					- offsetBetweenButtonAtPause / 2 - restart_btn.getHeight()
//					- offsetBetweenButtonAtPause - play_btn.getHeight(), null);
//			// gambar tombol reset
//			canvas.drawBitmap(restart_btn,
//					screenWidth / 2 - (restart_btn.getWidth() / 2),
//					screenHeight / 2 - offsetBetweenButtonAtPause / 2
//							- restart_btn.getHeight(), null);
//			// gambar tombol back to menu
//			canvas.drawBitmap(backToMenu_btn,
//					screenWidth / 2 - (backToMenu_btn.getWidth() / 2),
//					screenHeight / 2 + offsetBetweenButtonAtPause / 2, null);
//			// gambar tombol sound
//			if (WorldSetting.soundenabled) {
//				canvas.drawBitmap(sound_btn,
//						screenWidth / 2 - (sound_btn.getWidth() / 2),
//						screenHeight / 2 + offsetBetweenButtonAtPause / 2
//								+ backToMenu_btn.getHeight()
//								+ offsetBetweenButtonAtPause, null);
//			} else {
//				canvas.drawBitmap(soundOff_btn,
//						screenWidth / 2 - (soundOff_btn.getWidth() / 2),
//						screenHeight / 2 + offsetBetweenButtonAtPause / 2
//								+ backToMenu_btn.getHeight()
//								+ offsetBetweenButtonAtPause, null);
//			}

			canvas.setMatrix(matrix);
		}
	}

	private void drawBackGround(Canvas canvas) {
		canvas.drawColor(Color.BLACK);// clear screen
		paint.setFilterBitmap(true);
		paint.setDither(true);
		canvas.drawBitmap(mBackground, 0, 0, paint);
	}

	public void update() {
		if (state != PAUSE) {// jika tidak sedang pause
			currentTime += 5; //waktu terus bertambah sampai satu hari penuh 
			if (currentTime <= fullTime){
				Log.d("WAKTU", "waktu = "+currentTime);
				allPeople.update();
				// ganti activity. waktu habis
			}
//
//			if (!THROWING) {
//				allPeople.update();
//				if (WorldSetting.modegame == WorldSetting.BULETAN) {
//					BULETAN_RULES();
//				} else if (WorldSetting.modegame == WorldSetting.APOLLO) {
//					APOLLO_RULES();
//				} else if (WorldSetting.modegame == WorldSetting.DUEL){
//					DUEL_RULES();
//				}
//				else{
//					LATIHAN_RULES();
//				}
//
//				if (INITLEMPAR) {					
//					PowerLempar += 0.2;
//				}
//				if (preDoubleTap){
//					doubleTapCounter+=10;
//					//Log.d("double tap", "double tap dihitung");
//					if (doubleTapCounter>=tapMaxCounter){
//						doubleTapCounter = 0;
//						preDoubleTap = false;
//					//	Log.d("double tap", "double tap habis");
//					}
//					if (DoubleTaped){
//					//	Log.d("double tap", "double tap dilaksanakan");
//						
//						//slide focus ke gundu yang lagi giliran sekarang
//						mode = FOCUS;
//						//set sudut increment-nya
//						float[] tesm = new float[9];
//						matrix.getValues(tesm);
//						//set sudut slide focus
//						focusAngle = Math.atan2((-tesm[Matrix.MSCALE_Y]*allPeople.getPeople(Turn).y) - (tesm[Matrix.MTRANS_Y]-tesm[Matrix.MSCALE_Y]*screenHeight/2),(-tesm[Matrix.MSCALE_X]*allPeople.getPeople(Turn).x) - (tesm[Matrix.MTRANS_X]-tesm[Matrix.MSCALE_X]*screenWidth/2));
//
//						DoubleTaped = false;
//						doubleTapCounter = 0;
//						preDoubleTap = false;
//					}
//				}
//			}
		}
		if (onRestart) {// jika game akan direstart
			Log.d(TAG, "game akan direstart");
			restartGame();
			onRestart = false;
			state = RUNNING;
			Log.d(TAG, "game berhasil direstart");
		}
	}

	private void restartGame() {
		// restart gundu controller
		allPeople = new PeopleController(getContext(), getResources(),mBackground.getWidth(), mBackground.getHeight(),WorldSetting.modegame, WorldSetting.taruhan*WorldSetting.numPlayer);
		// restart operasi
		Operasi = new int[WorldSetting.numPlayer][2];
		for (int n = 0; n < WorldSetting.numPlayer; n++) {
			Operasi[n][0] = 0;// banyaknya kelereng yang telah didapat
		}
		for (int n = 0; n < WorldSetting.numPlayer; n++) {
			Operasi[n][1] = 1;// status 1 = masih hidup, status 0 sudah mati
		}
		// restart game state
		if (WorldSetting.modegame != WorldSetting.LATIHAN){
			GAME_STATE = INIT;
		}
		else{
			GAME_STATE = PLAY;
			Turn = 0;
			allPeople.ChangePeople(Turn, 1);
		}
		// restart dialog
		Dialog = DLEMPAR;
		// numofplayer
		numOfPlayer = WorldSetting.numPlayer;
		// mattin gambar tangan kalo tadinya lagi gambar tangan 
		RELEASE_SHOOT = false;
		// restart zoom ke paling kecil
		matrix.setScale(minScale, minScale);
	}
	private void APOLLO_RULES() {
		int gundukeluar;
		int countPeople;
    	if (GAME_STATE == INIT){
    		if (Dialog == DLEMPAR){
	    		if (WorldSetting.statusTutorial){
	    			mWorldActivity.showTutorial("Sentuh dan tahan layar di posisi yang sesuai untuk melempar People, perhatikan tenaga yang diberikan", "People yang tidak melewati garis bawah sebanyak 3 kali akan disimpan secara acak posisinya",H_GARISBAWAH);
	    		}
	    		else{
	    			ShowToast("Lempar People di posisi yang sesuai dan tahan tenaga");
	    		}
	    		Dialog = DMAIN;
    		}		
    	}
    	else if (GAME_STATE == PLAY) {
    		if (Dialog == DMAIN){
    			if (WorldSetting.statusTutorial){
    				mWorldActivity.showTutorial("Dalam permainan Apollo, semua People taruhan disimpan di dalam garis yang berbentuk roket",
    						"Pemain yang berhasil mengeluarkan People paling atas dari garis Apollo akan menjadi pemenang, bila tidak maka hanya akan mendapatkan People sebanyak yang keluar dihitung dari People yang keluar paling atas sampai paling bawah"
    						,"People yang melebihi garis bawah dari segitiga apollo, akan diletakkan kembali di garis bawah map"
    						,"Pemain tidak dapat melanjutkan permainan bila Peoplenya berhenti di dalam garis Apollo",H_GARISAPOLLO
    						);
    			}
	    		else{
	    			ShowToast("Apollo dimulai!!!");
	    		}
	    		Dialog = DDUEL;
    		}
			if ((RELEASE_SHOOT) && (allPeople.NoPeopleMoving())) {
				// FIXME : 1. ini nih kayanya kodingannya ga beres...
				if (allPeople.CheckApolloNP() != -9999) {
					if (allPeople.CheckApolloNP() != -9999){
						gundukeluar = allPeople.CheckApolloNP();
						countPeople = allPeople.CountPeopleNP();
						Log.d(TAG, "1Count People nih = "+allPeople.CountPeopleNP()+", gundu keluar="+gundukeluar);
						Log.d(TAG, "apolo player"+allPeople.CheckApolloP());
						if (allPeople.CheckApolloP()==(-9999)){
							Log.d(TAG, "2Count People nih = "+allPeople.CountPeopleNP()+", gundu keluar="+gundukeluar);
							//Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0] += (allPeople.CountPeopleNP() - allPeople.CheckApolloNP());
							if ((countPeople-gundukeluar) == 2){
								shouted=M_SHOUT;
				 				GameSound.startShout(getContext(),1);
							}
							else if ((countPeople-gundukeluar) ==3){
								shouted=P_SHOUT;
				 				GameSound.startShout(getContext(),2);
							}
							else if ((countPeople-gundukeluar) >3){
								shouted=H_SHOUT;
				 				GameSound.startShout(getContext(),3 );
							}
							while (allPeople.CheckApolloNP() != -9999) {
								if ((gundukeluar == 0)&& (GAME_STATE != ENDING)) {
									IDPemenang = allPeople.getPeople(Turn).isPlayer()-1;
									Log.d(TAG, "ID Pemenang Apollo = "+IDPemenang);
									GAME_STATE = ENDING;
									Log.d(TAG, "ENDING... karena cek apollo = "+gundukeluar);
									try {
										Thread.sleep(3000);
									} catch (InterruptedException e) {										
										e.printStackTrace();
									}
								}
								allPeople.Info();
								Log.d(TAG, "3Count People nih = "+countPeople+", gundu keluar="+gundukeluar);
								for(int z=0;z<(countPeople-gundukeluar);z++){
									Log.d(TAG, "cek apolo NP=" + gundukeluar);
									allPeople.listOfPeople.remove(gundukeluar);
									Turn-=1;
									Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0]+=1;
								}
							}
						}
						else{
							allPeople.PositioningApollo();
						}
					}
				}
				while (allPeople.CheckApolloP() != -9999) {
					ShowToast("Pemain "+WorldSetting.PlayingEmployee.get(allPeople.getPeople(allPeople.CheckApolloP()).isPlayer()-1).name+" keluar dari permainan");
					if (Turn > allPeople.PlayerOut()) {
						Turn -= 1;
					}
					Log.d(TAG, "Cek apollo P="+allPeople.CheckApolloP());
					Operasi[allPeople.getPeople(allPeople.CheckApolloP()).isPlayer()-1][1] = 0;
					allPeople.listOfPeople.remove(allPeople.CheckApolloP());
				}
				allPeople.PutBackPlayer();
				InfoHud();
				PlayerTurn();
				allPeople.ChangePeople(Turn, 1);
				RELEASE_SHOOT = false;
				Log.d(TAG, "bola player berhenti");
				allPeople.PositioningApollo();
				if (isOnePlayer()) {
						IDPemenang = allPeople.getPeople(Turn).isPlayer()-1;
						Log.d(TAG, "ID Pemenang Apollo = "+IDPemenang);
						Log.d(TAG, "si id = "+IDPemenang+", dapet "+allPeople.CountPeopleNP());
					GAME_STATE = ENDING;
					Log.d(TAG, "ENDING... karena gundu non player = "+allPeople.CountPeopleNP());
					try {
						Thread.sleep(3000);
					} catch (InterruptedException e) {						
						e.printStackTrace();
					}
				}
			}
		} else if (GAME_STATE == ENDING) {
			for (int h = 0; h < WorldSetting.numPlayer; h++) {
				if (h != (IDPemenang)) {
					if (Operasi[h][0] > 3) {
						Operasi[h][0] -= 3;
						Operasi[IDPemenang][0] += 3;
						Log.d(TAG, "si id = "+IDPemenang+", dapet 3");
					} else {
						Operasi[IDPemenang][0] += Operasi[h][0];
						Operasi[h][0] = 0;
						Log.d(TAG, "si id = "+IDPemenang+", dapet "+Operasi[h][0]);
					}
				} else {
					Operasi[IDPemenang][0] += allPeople
							.CountPeopleNP();
					Log.d(TAG, "si id = "+IDPemenang+", dapet "+allPeople.CountPeopleNP());
				}
			}

			Finish = true;
			if (!TMenang) {
				ShowToast("Pemenangnya "+WorldSetting.PlayingEmployee.get(IDPemenang).name+"!!!");
				TMenang = true;
			}
			mWorldActivity.Win();
		}
	}

	public boolean IsExistPlayer(int id) {
		boolean ketemu = false;
		if (delete.size() > 0) {
			for (int i = 0; i < delete.size(); i++) {
				if (delete.get(i) == id) {
					ketemu = true;
				}
			}
		}
		return ketemu;
	}

	private void DUEL_RULES() {
    	if (GAME_STATE == INIT){
//			mWorldActivity.showTutorial("Dalam permainan Duel, pemain secara berurutan menembak People pemain lain sampai hanya tinggal tersisa satu pemain sebagai pemenang");

//			mWorldActivity.showTutorial("Dalam permainan Apollo, semua People taruhan disimpan di dalam garis yang berbentuk roket");
//			highlight garis apollo
//			mWorldActivity.showTutorial("Pemain yang berhasil mengeluarkan People paling atas keluar garis Apollo akan menjadi pemenang, bila tidak maka hanya akan mendapatkan People sebanyak yang keluar dihitung dari People yang keluar paling atas sampai paling bawah");
//			mWorldActivity.showTutorial("Pemain akan mati bila berada di dalam garis Apollo");
    		
//			mWorldActivity.showTutorial("Dalam permainan Buletan, semua People taruhan disimpan di dalam Buletan");
//			highlight garis Buletan
//			mWorldActivity.showTutorial("Pemain akan mendapatkan People sebanyak People taruhan yang berhasil dia keluarkan dari Buletan");
//			mWorldActivity.showTutorial("Pemain akan mati bila berada di dalam Buletan ketika People taruhan masih tersisa");
//			mWorldActivity.showTutorial("Bila tidak ada lagi People taruhan yang tersisa di dalam Buletan, pemain harus saling berduel sampai hanya tinggal tersisa satu pemain sebagai pemenang ");
    		if (Dialog == DLEMPAR){
	    		if (WorldSetting.statusTutorial){
	    			mWorldActivity.showTutorial("Sentuh layar di posisi yang sesuai untuk meletakkan People", "People yang pertama diletakkan, mulai paling duluan");
	    		}
	    		else{
	    			ShowToast("Letakkan People di posisi yang sesuai");
	    		}
	    		Dialog = DMAIN;
    		}
    	}
    	else if (GAME_STATE == PLAY) {
    		if (Dialog == DMAIN){
    			if (WorldSetting.statusTutorial){
//    				mWorldActivity.showTutorial("Untuk menembak People, sentuh dan tarik People sebesar tenaga yang diinginkan");
    				mWorldActivity.showTutorial("Dalam permainan Duel, pemain secara berurutan menembak People pemain lain sampai hanya tinggal tersisa satu pemain sebagai pemenang","Duel dimulai!!!"
    						);
//    				highlight garis Buletan
//    				mWorldActivity.showTutorial("Pemain akan mendapatkan People sebanyak People taruhan yang berhasil dia keluarkan dari Buletan");
//    				mWorldActivity.showTutorial("Pemain akan mati bila berada di dalam Buletan ketika People taruhan masih tersisa");
//    				mWorldActivity.showTutorial("Bila tidak ada lagi People taruhan yang tersisa di dalam Buletan, pemain harus saling berduel sampai hanya tinggal tersisa satu pemain sebagai pemenang ");

    			}
	    		else{
	    			ShowToast("Duel Dimulai!!!");
	    		}
	    		Dialog = DDUEL;
    		}
			if ((allPeople.hit1 != -9999) && (allPeople.hit2 != -9999)) {
				if (Turn != allPeople.hit1) {
					Log.d(TAG, "nabrak si hit1 " + allPeople.hit1);
					if (!IsExistPlayer(allPeople.getPeople(allPeople.hit1)
							.isPlayer())) {						
						Operasi[allPeople.getPeople(allPeople.hit1).isPlayer() - 1][1] = 0;
						if (allPeople.getPeople(allPeople.hit1).isPlayer() > 0) {
							Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0] += Operasi[allPeople
									.getPeople(allPeople.hit1).isPlayer() - 1][0] + 1;
						} else {
							Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0] += +1;
						}
						Operasi[allPeople.getPeople(allPeople.hit1).isPlayer() - 1][0] = 0;
						delete.add(allPeople.getPeople(allPeople.hit1).isPlayer());
						Log.d(TAG,
								"tambah array delete "
										+ allPeople.getPeople(allPeople.hit1)
												.isPlayer());

					}
				}
				if (Turn != allPeople.hit2) {
					Log.d(TAG, "nabrak si hit2 " + allPeople.hit2);
					if (!IsExistPlayer(allPeople.getPeople(allPeople.hit2)
							.isPlayer())) {						
						Operasi[allPeople.getPeople(allPeople.hit2).isPlayer() - 1][1] = 0;
						if (allPeople.getPeople(allPeople.hit2).isPlayer() > 0) {
							Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0] += Operasi[allPeople
									.getPeople(allPeople.hit2).isPlayer() - 1][0] + 1;
						} else {
							Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0] += +1;
						}
						Operasi[allPeople.getPeople(allPeople.hit2).isPlayer() - 1][0] = 0;
						delete.add(allPeople.getPeople(allPeople.hit2).isPlayer());
						Log.d(TAG,
								"tambah array delete "
										+ allPeople.getPeople(allPeople.hit2)
												.isPlayer());
					}					
				}
				allPeople.hit1 = -9999;
				allPeople.hit2 = -9999;
			}
			if ((RELEASE_SHOOT) && (allPeople.NoPeopleMoving())) {				
				InfoHud();
				if (delete.size() > 0) {
					if (delete.size()==2){
						shouted=M_SHOUT;
		 				GameSound.startShout(getContext(), 1);
				}
					else if (delete.size()==3){
						shouted=P_SHOUT;
		 				GameSound.startShout(getContext(), 2);
				}
					else if (delete.size()>3){
						shouted=H_SHOUT;
		 				GameSound.startShout(getContext(), 3);
				}
					int deletingTurn = 0;
					for (int k = 0; k < delete.size(); k++) {
						ShowToast("Pemain "+WorldSetting.PlayingEmployee.get(allPeople.getPeople(allPeople.getIdPlayer(delete.get(k))).isPlayer()-1).name+" keluar dari permainan");
						if (Turn >= allPeople.getIdPlayer(delete.get(k))) {
							deletingTurn += 1;
						}
					}
					Turn -= deletingTurn;
					for (int k = 0; k < delete.size(); k++) {
						allPeople.Info();
						Log.d(TAG, "yang di delete player= " + delete.get(k));
						Log.d(TAG,
								" id = " + allPeople.getIdPlayer(delete.get(k)));
						allPeople.listOfPeople
								.remove(allPeople.getIdPlayer(delete.get(k)));
					}
					while (delete.size() != 0) {
						delete.remove(0);
					}
				}
				PlayerTurn();
				allPeople.ChangePeople(Turn, 1);
				RELEASE_SHOOT = false;
				Log.d(TAG, "bola player berhenti");
				if (allPeople.listOfPeople.size() == 1) {
					GAME_STATE = ENDING;
					IDPemenang = allPeople.getPeople(Turn).isPlayer()-1;
					Log.d(TAG, "ID Pemenang DUEL = "+IDPemenang);
				}
			}
		} else if (GAME_STATE == ENDING) {
			Log.d(TAG, "MENANG WOIIII");
			Finish = true;
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {										
				e.printStackTrace();
			}
			if (!TMenang) {
 				ShowToast("Pemenangnya "+WorldSetting.PlayingEmployee.get(IDPemenang).name+  "!!!");
				TMenang = true;
			}
			mWorldActivity.Win();
		}
	}
	//FIXME : duel buletan, taruhan udah habis trus playernya udah disentil, tapi gak ilang2(masih dapet giliran) ????
	//FIXME : urutan a,c,b. a jalan, c mati saat giliran c mati, malah ke a (mungkin karena yang jadi patokan adalah giliran pemilihan pemain)
	private void BULETAN_RULES() {
		int PeopleKeluar;
		int center_x, center_y;
    	double wx;
    	double wy;
    	//colliding balls (theorem of Pythagoras)
    	double distance;
    	if (GAME_STATE == INIT){
    		if (Dialog == DLEMPAR){
	    		if (WorldSetting.statusTutorial){
		    			mWorldActivity.showTutorial("Sentuh dan tahan layar di posisi yang sesuai untuk melempar People, perhatikan tenaga yang diberikan", "People yang tidak melewati garis bawah sebanyak 3 kali akan disimpan secara acak posisinya",H_GARISBAWAH);
	    		}
	    		else{
	    			ShowToast("Lempar People di posisi yang sesuai dan tahan tenaga");
	    		}
	    		Dialog = DMAIN;
    		}
    	}
    	else if (GAME_STATE == PLAY) {
    		if (Dialog == DMAIN){
    			if (WorldSetting.statusTutorial){    				
    				mWorldActivity.showTutorial("Dalam permainan Buletan, semua People taruhan disimpan di dalam Buletan","Pemain akan mendapatkan People sebanyak People taruhan yang berhasil dia keluarkan dari Buletan"
    						,"Pemain akan mati bila berada di dalam Buletan ketika People taruhan masih tersisa",
    						"Bila tidak ada lagi People taruhan yang tersisa di dalam Buletan, pemain harus saling berduel sampai hanya tinggal tersisa satu pemain sebagai pemenang ",
    						H_GARISBULETAN
    						);    			    			
    			}
	    		else{
					ShowToast("Buletan Dimulai!!!");
	    		}
    			
	    		Dialog = DDUEL;
    		}
			if ((RELEASE_SHOOT) && (allPeople.NoPeopleMoving())) {
				if (allPeople.PlayerInBuletan(Turn)){
			    	Random randomGenerator = new Random();
			    	while(allPeople.PeopleOutOfBuletan()!=-9999){
			    		center_x = randomGenerator.nextInt(281)-165;
			    		center_y = randomGenerator.nextInt(281)-250;
			    		wx=center_x+25;
			    		wy=center_y+110;
				    	//colliding balls (theorem of Pythagoras)
				    	distance=Math.sqrt(wx*wx+wy*wy);
				    	int SimpanPeople = allPeople.PeopleOutOfBuletan();
			    		if ((!allPeople.Tubrukan(center_x, center_y))&&(distance<140)){
			    	    	allPeople.getPeople(SimpanPeople).x = (mBackground.getWidth()/2)+center_x;
			    	    	allPeople.getPeople(SimpanPeople).y = (mBackground.getHeight()/3)+center_y;			    	    	
			    		}

			    	}
				}
				else{
					PeopleKeluar = allPeople.CountPeopleOut();
					if (PeopleKeluar==2){
		 				shouted=M_SHOUT;
		 				GameSound.startShout(getContext(),1);
				}
					else if (PeopleKeluar==3){
						shouted=P_SHOUT;
		 				GameSound.startShout(getContext(),2);
				}
					else if (PeopleKeluar>3){
						shouted=H_SHOUT;
		 				GameSound.startShout(getContext(),3);
			}
					Operasi[allPeople.getPeople(Turn).isPlayer() - 1][0] += PeopleKeluar;
					allPeople.RemovePeopleOut();
					Turn -= PeopleKeluar;
				}
				while (allPeople.PlayerOut() != -9999) {
					Log.d(TAG,
							"MASUK -9999 = "
									+ (allPeople.getPeople(allPeople.PlayerOut())
											.isPlayer() - 1) + ", ="
									+ allPeople.PlayerOut());
					ShowToast("Pemain "+WorldSetting.PlayingEmployee.get(allPeople.getPeople(allPeople.PlayerOut()).isPlayer()-1).name+" keluar dari permainan");
					Operasi[allPeople.getPeople(allPeople.PlayerOut()).isPlayer() - 1][1] = 0;
					if (Turn > allPeople.PlayerOut()) {
						Turn -= 1;
					}
					allPeople.listOfPeople.remove(allPeople.PlayerOut());
				}
				InfoHud();
				PlayerTurn();
				allPeople.ChangePeople(Turn, 1);
				RELEASE_SHOOT = false;
				Log.d(TAG, "bola player berhenti");
				if (allPeople.noPeopleBuletan()) {
					GAME_STATE = ENDING;
					allPeople.hit1 = -9999;
					allPeople.hit2 = -9999;
					Log.d(TAG, "DUEL TIME");
//					if (!TDuel) {
//						ShowToast("DUEL TIME!!!");
//						TDuel = true;
//					}
				}
				BuletanWin();
			}
		} else if (GAME_STATE == ENDING) {
			if (Dialog==DDUEL){
				if (WorldSetting.statusTutorial){
	    			mWorldActivity.showTutorial("People taruhan telah habis, semua pemain saling menembak satu sama lain sampai hanya tinggal tersisa satu pemain sebagai pemenang");
	    		}
	    		else{
    				ShowToast("Duel dimulai!!!");
	    		}
	    		Dialog = NONE;
			}
			if ((allPeople.hit1 != -9999) && (allPeople.hit2 != -9999)) {
				if (Turn != allPeople.hit1) {
					Log.d(TAG, "nabrak si hit1 " + allPeople.hit1);
					if (!IsExistPlayer(allPeople.getPeople(allPeople.hit1)
							.isPlayer())) {
						Operasi[allPeople.getPeople(allPeople.hit1).isPlayer() - 1][1] = 0;
						delete.add(allPeople.getPeople(allPeople.hit1).isPlayer());
						Log.d(TAG,
								"tambah array delete "
										+ allPeople.getPeople(allPeople.hit1)
												.isPlayer());
					}
				}
				if (Turn != allPeople.hit2) {
					Log.d(TAG, "nabrak si hit2 " + allPeople.hit2);
					if (!IsExistPlayer(allPeople.getPeople(allPeople.hit2)
							.isPlayer())) {						
						Operasi[allPeople.getPeople(allPeople.hit2).isPlayer() - 1][1] = 0;
						delete.add(allPeople.getPeople(allPeople.hit2).isPlayer());
						Log.d(TAG,
								"tambah array delete "
										+ allPeople.getPeople(allPeople.hit2)
												.isPlayer());
					}
					// allPeople.listOfPeople.remove(allPeople.hit2);
				}
				allPeople.hit1 = -9999;
				allPeople.hit2 = -9999;
			}
			if ((RELEASE_SHOOT) && (allPeople.NoPeopleMoving())) {
				// Operasi[allPeople.getPeople(Turn).isPlayer()-1][0]=PeopleKeluar;
				InfoHud();
				if (delete.size() > 0) {
					int deletingTurn = 0;
					for (int k = 0; k < delete.size(); k++) {
						if (Turn >= allPeople.getIdPlayer(delete.get(k))) {
							deletingTurn += 1;
						}
						ShowToast("Pemain "+WorldSetting.PlayingEmployee.get(allPeople.getPeople(allPeople.getIdPlayer(delete.get(k))).isPlayer()-1).name+" keluar dari permainan");
					}
					Turn -= deletingTurn;
					for (int k = 0; k < delete.size(); k++) {
						if (Turn >= allPeople.getIdPlayer(delete.get(k))) {
							Turn -= 1;
						}
						allPeople.Info();
						Log.d(TAG, "yang di delete player= " + delete.get(k));
						Log.d(TAG,
								" id = " + allPeople.getIdPlayer(delete.get(k)));
						allPeople.listOfPeople
								.remove(allPeople.getIdPlayer(delete.get(k)));
					}
					while (delete.size() != 0) {
						delete.remove(0);
					}
				}
				PlayerTurn();
				allPeople.ChangePeople(Turn, 1);
				RELEASE_SHOOT = false;
				Log.d(TAG, "bola player berhenti");
				BuletanWin();
			}
		}
	}

	public void BuletanWin() {
		if ((allPeople.listOfPeople.size() == 1) || (isOnePlayer())) {
			Log.d(TAG, "MENANG");
			if (allPeople.FirstPlayer() != -9999) {
				IDPemenang = allPeople.getPeople(Turn).isPlayer()-1;
				Log.d(TAG, "ID Pemenang buletan = "+IDPemenang);
			}
			if (!TMenang) {
 				ShowToast("Pemenangnya "+WorldSetting.PlayingEmployee.get(IDPemenang).name+  "!!!");
				TMenang = true;
			}
			for (int h = 0; h < WorldSetting.numPlayer; h++) {
				if (h != (IDPemenang)) {
					if (Operasi[h][0] > 3) {
						Operasi[h][0] -= 3;
						Operasi[IDPemenang][0] += 3;
						Log.d(TAG, "si id = "+IDPemenang+", dapet 3");
					} else {
						Operasi[IDPemenang][0] += Operasi[h][0];
						Operasi[h][0] = 0;
						Log.d(TAG, "si id = "+IDPemenang+", dapet "+Operasi[h][0]);
					}
				} else {
					Operasi[IDPemenang][0] += allPeople
							.CountPeopleNP();
					Log.d(TAG, "si id = "+IDPemenang+", dapet "+allPeople.CountPeopleNP());
				}
			}
			Finish = true;
			InfoHud();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {										
				e.printStackTrace();
			}
			mWorldActivity.Win();
		}
	}
	public void LATIHAN_RULES(){
		if (Dialog == DLEMPAR){
				mWorldActivity.showTutorial("Untuk melakukan zoom-in, sentuh layar dengan dua jari dan tarik bersamaan menuju satu titik, dan lakukan sebaliknya untuk zoom-out",
						"Sentuh dua kali dengan cepat untuk menemukan People Anda",
						"Geser jari anda di layar untuk menggeser peta",
						"People yang sedang mendapatkan giliran adalah gundu yang diberi tanda kelap-kelip",
						"Untuk menembak People, sentuh gundu dan tarik ke belakang sejauh tenaga yang diinginkan"
				);    			    			
    		Dialog = NONE;
		}
		if ((RELEASE_SHOOT) && (allPeople.NoPeopleMoving())) {
			allPeople.ChangePeople(Turn, 1);
			RELEASE_SHOOT = false;
		}
	}
	public void InfoHud() {
		for (int h = 0; h < WorldSetting.numPlayer; h++) {

			Log.d(TAG, "player = " + h + ", bola = " + Operasi[h][0]
					+ ", status=" + Operasi[h][1]);

		}
	}

	public boolean isOnePlayer() {
		boolean truthy = false;
		if ((allPeople.listOfPeople.size() - allPeople.CountPeopleNP()) == 1)
			truthy = true;
		return truthy;
	}

	private void drawallPeople(Canvas canvas) {
		for (int i = 0; i < allPeople.listOfPeople.size(); i++) {
			allPeople.getPeople(i).draw(canvas);
		}
	}
	private void initializeApatar() {
		float scale = 0.7f;
		int widthapatar = 50;
		int heightapatar = 60;
		apatar_lib = new Bitmap[5];
//		apatar_lib[0] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.apatarmini0)
//				, widthapatar, heightapatar, false);
//		apatar_lib[1] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.apatarmini1)
//				, widthapatar, heightapatar, false);
//		apatar_lib[2] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.apatarmini2)
//				, widthapatar, heightapatar, false);
//		apatar_lib[3] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.apatarmini3)
//				, widthapatar, heightapatar, false);
//		apatar_lib[4] = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.apatarmini4)
//				, widthapatar, heightapatar, false);
//		for (int i = 0; i < WorldSetting.numPlayer;i++){//(allPeople.listOfPeople.size() - allPeople.CountPeopleNP()); i++) {
//			hudImage[i] = apatar_lib[WorldSetting.PlayingEmployee.get(i).avatar];
//		}
		

	}
	private void initializeShoutBitmap() {
		animShouter = new Bitmap[3][10];
//    	animShouter[0][0] = BitmapFactory.decodeResource(getResources(), R.drawable.m1);
//    	animShouter[0][1] = BitmapFactory.decodeResource(getResources(), R.drawable.m2);
//    	animShouter[0][2] = BitmapFactory.decodeResource(getResources(), R.drawable.m3);
//    	animShouter[0][3] = BitmapFactory.decodeResource(getResources(), R.drawable.m4);
//    	animShouter[0][4] = BitmapFactory.decodeResource(getResources(), R.drawable.m5);
//    	animShouter[0][5] = BitmapFactory.decodeResource(getResources(), R.drawable.m6);
//    	animShouter[1][0] = BitmapFactory.decodeResource(getResources(), R.drawable.p1);
//    	animShouter[1][1] = BitmapFactory.decodeResource(getResources(), R.drawable.p2);
//    	animShouter[1][2] = BitmapFactory.decodeResource(getResources(), R.drawable.p3);
//    	animShouter[1][3] = BitmapFactory.decodeResource(getResources(), R.drawable.p4);
//    	animShouter[1][4] = BitmapFactory.decodeResource(getResources(), R.drawable.p5);
//    	animShouter[1][5] = BitmapFactory.decodeResource(getResources(), R.drawable.p6);
//    	animShouter[2][0] = BitmapFactory.decodeResource(getResources(), R.drawable.h1);
//    	animShouter[2][1] = BitmapFactory.decodeResource(getResources(), R.drawable.h2);
//    	animShouter[2][2] = BitmapFactory.decodeResource(getResources(), R.drawable.h3);
//    	animShouter[2][3] = BitmapFactory.decodeResource(getResources(), R.drawable.h4);
//    	animShouter[2][4] = BitmapFactory.decodeResource(getResources(), R.drawable.h5);
//    	animShouter[2][5] = BitmapFactory.decodeResource(getResources(), R.drawable.h6);

	}
	private void initializeHighlightBitmap() {
		cTimeAnimHighlight = 0;
		animBatasBawah = new Bitmap[7];
		cAnimBatasBawah = 0;
//		animBatasBawah[0] = BitmapFactory.decodeResource(getResources(), R.drawable.g1);
//		animBatasBawah[1] = BitmapFactory.decodeResource(getResources(), R.drawable.g2);
//		animBatasBawah[2] = BitmapFactory.decodeResource(getResources(), R.drawable.g3);
//		animBatasBawah[3] = BitmapFactory.decodeResource(getResources(), R.drawable.g4);
//		animBatasBawah[4] = BitmapFactory.decodeResource(getResources(), R.drawable.g5);
//		animBatasBawah[5] = BitmapFactory.decodeResource(getResources(), R.drawable.g6);
//		animBatasBawah[6] = BitmapFactory.decodeResource(getResources(), R.drawable.g7);	
//		animBuletan = new Bitmap[9];
//		cAnimBuletan = 0;
//		animBuletan[0] = BitmapFactory.decodeResource(getResources(), R.drawable.b1);
//		animBuletan[1] = BitmapFactory.decodeResource(getResources(), R.drawable.b2);
//		animBuletan[2] = BitmapFactory.decodeResource(getResources(), R.drawable.b3);
//		animBuletan[3] = BitmapFactory.decodeResource(getResources(), R.drawable.b4);
//		animBuletan[4] = BitmapFactory.decodeResource(getResources(), R.drawable.b5);
//		animBuletan[5] = BitmapFactory.decodeResource(getResources(), R.drawable.b6);
//		animBuletan[6] = BitmapFactory.decodeResource(getResources(), R.drawable.b7);
//		animBuletan[7] = BitmapFactory.decodeResource(getResources(), R.drawable.b8);
//		animBuletan[8] = BitmapFactory.decodeResource(getResources(), R.drawable.b9);
//		animRoketApollo = new Bitmap[7];
//		cAnimRoketApollo = 0;
//		animRoketApollo[0] = BitmapFactory.decodeResource(getResources(), R.drawable.r1);
//		animRoketApollo[1] = BitmapFactory.decodeResource(getResources(), R.drawable.r2);
//		animRoketApollo[2] = BitmapFactory.decodeResource(getResources(), R.drawable.r3);
//		animRoketApollo[3] = BitmapFactory.decodeResource(getResources(), R.drawable.r4);
//		animRoketApollo[4] = BitmapFactory.decodeResource(getResources(), R.drawable.r5);
//		animRoketApollo[5] = BitmapFactory.decodeResource(getResources(), R.drawable.r6);
//		animRoketApollo[6] = BitmapFactory.decodeResource(getResources(), R.drawable.r7);
//		animSegitigaApollo = new Bitmap[7];
//		cAnimSegitigaApollo = 0;
//		animSegitigaApollo[0] = BitmapFactory.decodeResource(getResources(), R.drawable.k1);
//		animSegitigaApollo[1] = BitmapFactory.decodeResource(getResources(), R.drawable.k2);
//		animSegitigaApollo[2] = BitmapFactory.decodeResource(getResources(), R.drawable.k3);
//		animSegitigaApollo[3] = BitmapFactory.decodeResource(getResources(), R.drawable.k4);
//		animSegitigaApollo[4] = BitmapFactory.decodeResource(getResources(), R.drawable.k5);
//		animSegitigaApollo[5] = BitmapFactory.decodeResource(getResources(), R.drawable.k6);
//		animSegitigaApollo[6] = BitmapFactory.decodeResource(getResources(), R.drawable.k7);
	}
	
	/*
	 * fungsi ini untuk meng-highlight garis tertentu di dalam mode tutorial
	 * highlightType diisi konstanta :
	 * GARIS_BATAS_BAWAH
	 * GARIS_BULETAN
	 * GARIS_SEGITIGA_APOLLO
	 * GARIS_ROKET_APOLLO
	 */
	public void highlightGame(int highlightType) {
		cHighlight = 0;		
		switch(highlightType) {
		case GARIS_BATAS_BAWAH :
			statusHighlightGarisBatasBawah = true;
			break;
		case GARIS_BULETAN :
			statusHighlightGarisBuletan = true;
			break;
		case GARIS_SEGITIGA_APOLLO :
			statusHighlightGarisSegitigaApollo = true;
			break;
		case GARIS_ROKET_APOLLO :
			statusHighlightGarisRoketApollo = true;
			break;
		}
	}
	
	private void drawHighlight(Canvas canvas) {
		if(cHighlight>=maxCounterHighlight) {
			statusHighlightGarisBatasBawah = false;
			statusHighlightGarisBuletan = false;
			statusHighlightGarisRoketApollo = false;
			statusHighlightGarisSegitigaApollo = false;
			cHighlight = 0;
			cTimeAnimHighlight = 0;
			cAnimBatasBawah = 0;
			cAnimBuletan = 0;
			cAnimRoketApollo = 0;
			cAnimSegitigaApollo = 0;
		}		
		//gambar per garis highlight
		if(statusHighlightGarisBatasBawah) {
			//gambar garis batas bawah
			Log.d(TAG,"gambar garis batas bawah" + cAnimBatasBawah);			
			canvas.drawBitmap(animBatasBawah[cAnimBatasBawah], 50, 1100, null);								
		}
		if(statusHighlightGarisBuletan) {
			//gambar garis buletan
			Log.d(TAG,"gambar garis buletan : " + cAnimBuletan);
			canvas.drawBitmap(animBuletan[cAnimBuletan], 290, 220, null);		
		}
		if(statusHighlightGarisRoketApollo) {
			//gambar garis roket apollo
			Log.d(TAG,"gambar garis roket apollo : " + cAnimRoketApollo);
			canvas.drawBitmap(animRoketApollo[cAnimRoketApollo], 375, 43, null);		
		}
		if(statusHighlightGarisSegitigaApollo) {
			//gambar garis segitiga apollo
			Log.d(TAG,"gambar garis segitiga apollo : " + cAnimSegitigaApollo);
			canvas.drawBitmap(animSegitigaApollo[cAnimSegitigaApollo], 375, 43, null);										
		}
		//increment cHighlight jika salah satu dari kondisi statusHightLight true
		if(statusHighlightGarisBatasBawah || statusHighlightGarisBuletan || statusHighlightGarisRoketApollo || statusHighlightGarisSegitigaApollo) {
			cHighlight++;
			cTimeAnimHighlight++;
			if(cTimeAnimHighlight>=timeBetweenAnimHighlight) {
				cTimeAnimHighlight = 0;
				if(statusHighlightGarisBatasBawah) {
					cAnimBatasBawah = (cAnimBatasBawah+1) % 7;
				}
				if(statusHighlightGarisBuletan) {
					cAnimBuletan = (cAnimBuletan+1) % 9;
				}
				if(statusHighlightGarisRoketApollo) {
					cAnimRoketApollo = (cAnimRoketApollo+1) % 7;
				}
				if(statusHighlightGarisSegitigaApollo) {
					cAnimSegitigaApollo = (cAnimSegitigaApollo+1) % 7;
				}
			}
		}
	}
	
	private void moveCamera(int x, int y, int scale) {
		matrix.setScale(scale, scale);
		matrix.postTranslate(-scale*x,-scale*y);
		fixScreen();//jika pergeseran melebihi batas map(keliatan hitam di luar daerah peta) 
	}
	
	//x dan y adalah kordinat mesin
	private void moveCamera(int x, int y) {
		float[] tesm = new float[9];
		matrix.getValues(tesm);
		matrix.setScale(tesm[Matrix.MSCALE_X], tesm[Matrix.MSCALE_Y]);
		matrix.postTranslate(-tesm[Matrix.MSCALE_X]*x, -tesm[Matrix.MSCALE_Y]*y);		
		fixScreen();//jika pergeseran melebihi batas map(keliatan hitam di luar daerah peta)
	}
	
	private void focusSlideToPeople() {
		if(mode==WAIT_TO_FOCUS) {//lagi nunggu, tambahin counter			
			cWaitFocus++;			
			if(cWaitFocus>waitFocusTime) {//kalo sudah waktunya untuk slide focus
				mode=FOCUS;
				cWaitFocus = 0;//restart counter
			}
		}
		else if(mode==FOCUS && (GAME_STATE==PLAY || GAME_STATE==ENDING)) {
			float[] tesm = new float[9];
			matrix.getValues(tesm);			
			//Log.d(TAG,"kordinat kamera : "+tesm[Matrix.MTRANS_X]+","+tesm[Matrix.MTRANS_Y]);
			//Log.d(TAG,"kordinat gundu : " + (-tesm[Matrix.MSCALE_X]*allPeople.getPeople(Turn).x) + "," + (-tesm[Matrix.MSCALE_Y]*allPeople.getPeople(Turn).y));
			
			if(Math.abs((tesm[Matrix.MTRANS_X]-tesm[Matrix.MSCALE_X]*screenWidth/2) - (-tesm[Matrix.MSCALE_X]*allPeople.getPeople(Turn).x))<30 && 
					Math.abs((tesm[Matrix.MTRANS_Y]-tesm[Matrix.MSCALE_Y]*screenHeight/2) - (-tesm[Matrix.MSCALE_Y]*allPeople.getPeople(Turn).y))<30) {
				//jika sudah nyampe di gundu yang lagi giliran
				mode=NONE;//jangan slide focus lagi
				fixScreen();
			}else{
				float Vx = (float) (focusV * Math.cos(focusAngle));
				float Vy = (float) (focusV * Math.sin(focusAngle));
				if(Math.abs((tesm[Matrix.MTRANS_X]-tesm[Matrix.MSCALE_X]*screenWidth/2) - (-tesm[Matrix.MSCALE_X]*allPeople.getPeople(Turn).x))>=30) {
					matrix.postTranslate(Vx,0);
				}
				if(Math.abs((tesm[Matrix.MTRANS_Y]-tesm[Matrix.MSCALE_Y]*screenHeight/2) - (-tesm[Matrix.MSCALE_Y]*allPeople.getPeople(Turn).y))>=30) {
					matrix.postTranslate(0,Vy);
				}
				Log.d(TAG,"Vx : " + Vx + " dan Vy : " + Vy);
				Log.d(TAG,"focusAngle : "+focusAngle);				
				fixScreen();
			}
		}		
	}
}
